package com.spider.iceCream.controller;

import com.spider.iceCream.BarrageSpider;
import com.spider.iceCream.CommentSpider;
import com.spider.iceCream.core.Result;
import com.spider.iceCream.entity.*;
import com.spider.iceCream.service.DayRankService;
import com.spider.iceCream.service.VideoService;
import com.spider.iceCream.spiderComponent.Barrage;
import com.spider.iceCream.spiderComponent.GetBarrage;
import com.spider.iceCream.spiderComponent.model.Rank;
import com.spider.iceCream.spiderComponent.parse.GetReply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Lord
 * @date 2021/7/13 9:27
 */
@RestController
@RequestMapping("/video")
@Api(tags = "视频相关操作")
@CrossOrigin
public class RankController {
    @Autowired
    VideoService videoService;
    @Autowired
    DayRankService dayRankService;

    @ResponseBody

    @ApiOperation(value = "获取视频弹幕评论详细信息",notes = "根据视频oid和cid（通过榜单展示可以获取这两个）获取视频弹幕评论详细信息")
    @RequestMapping(value = "/getInfo",method = RequestMethod.GET)
    @CrossOrigin
    public Result getVideoInfo(@RequestParam Integer oid,
                               @RequestParam Integer cid
    ) throws Exception {
        Result result = new Result();
//        System.out.println(getDate()+"-获取评论和弹幕");
        VideoInfo info = new VideoInfo();
        info.setRankHistory(dayRankService.getRankHistory(oid));
        try{
            ExecutorService executorService = Executors.newFixedThreadPool(5);
            List<Barrage> barrages = new ArrayList<>();
            Comments comments = new Comments();
            Callable barrage = new BarrageSpider(cid);
            Callable comment = new CommentSpider(oid);
            try {
                Future<List<Barrage>> sub1 = executorService.submit(barrage);
                Future<Comments> sub2 = executorService.submit(comment);
                barrages = sub1.get();
                comments = sub2.get();
            }catch (ExecutionException e){
                e.printStackTrace();
            }
            executorService.shutdown();
            while (true){
                if (executorService.isTerminated()){
                    info.setBarrages(barrages);
                    info.setComments(comments);
//                    String content = "";
//                    for (Barrage b :
//                            barrages) {
//                        content+=b.getMessage();
//                    }
//                    int topN=5;
//                    TFIDFAnalyzer tfidfAnalyzer=new TFIDFAnalyzer();
//                    List<Keyword> list=tfidfAnalyzer.analyze(content,topN);
//                    List<BKeyWord> data = new ArrayList<>();
//                    for(Keyword word:list){
//                        BKeyWord b = new BKeyWord(word.getName(),word.getTfidfvalue());
//                        data.add(b);
//                    }
//                    info.setKeyWords(data);
                    result.setCode(200);
                    result.setMessage("查询成功");
                    result.setData(info);
                    break;
                }
            }
//            GetBarrage getBarrage = new GetBarrage();
//            GetReply getReply = new GetReply();
//            Comments comments = new Comments();
//            info.setBarrages(getBarrage.getBarrages(cid));
//            comments.setComments(getReply.allReply(oid));
//            comments.setLength(comments.getComments().size());
//            info.setComments(comments);

        } catch (Exception e){
            System.out.println("异常："+e);
            e.printStackTrace();
            result.setCode(401);
            result.setMessage("查询失败");
        }
        return result;
    }
    public String getDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern("yyyy-MM-dd");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    @ApiOperation(value = "获取某日榜单",notes = "通过日期和类别获取榜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category",value = "类别",defaultValue = "all"),
            @ApiImplicitParam(name = "date",value = "日期",defaultValue = "2021-7-16")
    })
    @RequestMapping(value = "/getRank",method = RequestMethod.GET)
    @CrossOrigin
    public Result getRank(@RequestParam String category,
                          @RequestParam String date
    ){
//        System.out.println("获取某日某类别排行榜");
        Result result = new Result();
        DayRank rank = new DayRank();
        List<RankInfo> data = new ArrayList<>();
        rank.setCategory(category);
        rank.setRankDate(date);
        List<DayRank> rankList = dayRankService.getRank(rank);
        for (DayRank d :
                rankList) {
            RankInfo r = new RankInfo();
            List<DayRank> l = new ArrayList<>();
            l.add(d);
            r.setDayRank(l);
            r.setVideo(videoService.isExistVideo(d.getOid()));
            data.add(r);
        }
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(data);
        return result;
    }

    @ApiOperation(value = "通过BV号或者关键词搜索曾在榜单出现的视频")
    @ApiImplicitParam(name = "key",value = "关键词或bv号")
    @RequestMapping(value = "/searchVideo",method = RequestMethod.GET)
    @CrossOrigin
    public Result search(@RequestParam String key) throws Exception{
        Result result = new Result();
        List<RankInfo> list = new ArrayList<>();
        try {
            List<Video> videos = videoService.getVideoByBV(key);
            for (Video v :
                    videos) {
                List<DayRank> d = new ArrayList<>();
                d = dayRankService.getSingleRank(v.getOid(),getDate());
                RankInfo info = new RankInfo(d,v);
                list.add(info);

            }
            result.setData(list);
            result.setMessage("查询成功");
            result.setCode(200);
        }catch (Exception e){
            result.setMessage("查询失败");
            result.setCode(401);
            e.printStackTrace();
        }
        return result;
    }
}
